//
//  DeepLinkTool.m
//  Rail
//
//  Created by hychen on 2018/5/28.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "DeepLinkTool.h"

@interface DeepLinkTool ()
@property (nonatomic, strong) NSMutableArray *urlArray;
@end
@implementation DeepLinkTool

SingletonM_lib(Tool)

- (instancetype)init {
    if (self = [super init]) {
        [self prepareNotifications];
    }
    return self;
}

- (void)prepareNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:Notify_Login_Success object:nil];
}

- (void)loginSuccess {
    if (self.urlArray.count > 0) {
        NSString *url = self.urlArray.firstObject;
        [self.urlArray removeObjectAtIndex:0];
        if ([url isKindOfClass:[NSString class]]) {
            [self handleDeepLinkUrl:[NSURL URLWithString:url]];
        }
    }
}

- (BOOL)handleDeepLinkUrl:(NSURL *)url {
    
    NSString *urlString = [url absoluteString];
    
    //iscs
    NSString *scheme = [url scheme];
    if (![scheme isEqualToString:@"iscs"]) {
        return NO;
    }
    
    //lient.iscs.just.com
    NSString *host = [url host];
    if (![host isEqualToString:@"client.iscs.just.com"]) {
        return NO;
    }
    
    //DeepLink
    //判断是否登录, 未登录则进行登录后再跳转
    BOOL isLogin = [GlobalManage sharedManage].userInfoModel == nil?NO:YES;
    
    if ([urlString containsString:kWebDeepLinkArea]) {
//        if ([JumpTool isCheckUserLoginWithTarget:self Action:@selector(handleDeepLinkUrl:) Object:url rootVC:[JumpTool getCurrentViewController]]) {
//
//            NSString *areaId = [WebHelper getJumpValueWithUrl:urlString type:@"1"];
//            if ([areaId objectIsNotEmpty]) {
//                [JumpTool gotoWorkAreaDetailWithAreaId:areaId navTitle:@"工区详情" vc:[JumpTool getCurrentViewController]];
//            }
//            return YES;
//        }
        
        NSString *areaId = [WebHelper getJumpValueWithUrl:urlString type:@"1"];
        if ([areaId objectIsNotEmpty]) {
            if (isLogin) {
                [JumpTool gotoWorkAreaDetailWithAreaId:areaId navTitle:@"工区详情" vc:[JumpTool getCurrentViewController]];
                return YES;
            }
            else {
                [self.urlArray addObject:urlString];
            }
        }
        return NO;
    }
    
    if ([urlString containsString:kWebDeepLinkStation]) {
//        if ([JumpTool isCheckUserLoginWithTarget:self Action:@selector(handleDeepLinkUrl:) Object:url rootVC:[JumpTool getCurrentViewController]]) {
//
//            NSString *stationId = [WebHelper getJumpValueWithUrl:urlString type:@"2"];
//            if ([stationId objectIsNotEmpty]) {
//                [JumpTool gotoStationDetailWithStationId:stationId vc:[JumpTool getCurrentViewController]];
//            }
//            return YES;
//        }

        NSString *stationId = [WebHelper getJumpValueWithUrl:urlString type:@"2"];
        if ([stationId objectIsNotEmpty]) {
            if (isLogin) {
                [JumpTool gotoStationDetailWithStationId:stationId vc:[JumpTool getCurrentViewController]];
                return YES;
            }
            else {
                [self.urlArray addObject:urlString];
            }
        }
        return NO;
    }
    
    if ([urlString containsString:kWebDeepLinkSection]) {
//        if ([JumpTool isCheckUserLoginWithTarget:self Action:@selector(handleDeepLinkUrl:) Object:url rootVC:[JumpTool getCurrentViewController]]) {
//
//            NSString *sectionId = [WebHelper getJumpValueWithUrl:urlString type:@"3"];
//            if ([sectionId objectIsNotEmpty]) {
//                [JumpTool gotoSectionDetailWithSectionId:sectionId vc:[JumpTool getCurrentViewController]];
//            }
//            return YES;
//        }

        NSString *sectionId = [WebHelper getJumpValueWithUrl:urlString type:@"3"];
        if ([sectionId objectIsNotEmpty]) {
            if (isLogin) {
                [JumpTool gotoSectionDetailWithSectionId:sectionId vc:[JumpTool getCurrentViewController]];
                return YES;
            }
            else {
                [self.urlArray addObject:urlString];
            }
        }
        return NO;
    }
    
    if ([urlString containsString:kWebDeepLinkLine]) {
//        if ([JumpTool isCheckUserLoginWithTarget:self Action:@selector(handleDeepLinkUrl:) Object:url rootVC:[JumpTool getCurrentViewController]]) {
//
//            NSString *lineId = [WebHelper getJumpValueWithUrl:urlString type:@"4"];
//            if ([lineId objectIsNotEmpty]) {
//                [JumpTool gotoLineDetailWithLineId:lineId vc:[JumpTool getCurrentViewController]];
//            }
//            return YES;
//        }

        NSString *lineId = [WebHelper getJumpValueWithUrl:urlString type:@"4"];
        if ([lineId objectIsNotEmpty]) {
            if (isLogin) {
                [JumpTool gotoLineDetailWithLineId:lineId vc:[JumpTool getCurrentViewController]];
                return YES;
            }
            else {
                [self.urlArray addObject:urlString];
            }
        }
        return NO;
    }

    return NO;
}

- (NSMutableArray *)urlArray {
    if (!_urlArray) {
        _urlArray = [NSMutableArray array];
    }
    return _urlArray;
}
@end
